package com.vecspace.framework.sysdata.dao;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.vecspace.data.model.RowMapper;
import com.vecspace.database.dao.AbstractDao;
import com.vecspace.framework.sysdata.pojo.DataLog;

@Repository("sys_data_log_dao")
public class SysDataLogDao extends AbstractDao{

	public void addLog(Long tableId, Long dataId, Long columnId, String oldValue, String newValue, Long batchNum, Long userId) throws Exception {
		String sql = "insert into tm_sys_data_log(TABLE_ID,DATA_ID,COLUMN_ID,OLD_VALUE_STR,NEW_VALUE_STR,BATCH_NUM,UPDATE_TIME,UPDATE_USER) values(?,?,?,?,?,?,?,?)";
		dao.insertDataRowBySql(sql, new Object[] {tableId, dataId, columnId, oldValue, newValue, batchNum,new Date(),userId});
	}

	public void addClobLog(Long tableId, Long dataId, Long columnId, String oldValue, String newValue, Long batchNum, Long userId) throws Exception {
		String sql = "insert into tm_sys_data_log(TABLE_ID,DATA_ID,COLUMN_ID,OLD_VALUE_TEXT,NEW_VALUE_TEXT,BATCH_NUM,UPDATE_TIME,UPDATE_USER) values(?,?,?,?,?,?,?,?)";
		dao.insertDataRowBySql(sql, new Object[] {tableId, dataId, columnId, oldValue, newValue, batchNum, new Date(), userId});
	}

	public void addDateLog(Long tableId, Long dataId, Long columnId, LocalDate oldValue, LocalDate newValue, Long batchNum, Long userId) throws Exception {
		String sql = "insert into tm_sys_data_log(TABLE_ID,DATA_ID,COLUMN_ID,OLD_VALUE_DATE,NEW_VALUE_DATE,BATCH_NUM,UPDATE_TIME,UPDATE_USER) values(?,?,?,?,?,?,?,?)";
		dao.insertDataRowBySql(sql, new Object[] {tableId, dataId, columnId, oldValue, newValue, batchNum, new Date(), userId});
	}
	
	public void addDateTimeLog(Long tableId, Long dataId, Long columnId, LocalDateTime oldValue, LocalDateTime newValue, Long batchNum, Long userId) throws Exception {
		String sql = "insert into tm_sys_data_log(TABLE_ID,DATA_ID,COLUMN_ID,OLD_VALUE_TIME,NEW_VALUE_TIME,BATCH_NUM,UPDATE_TIME,UPDATE_USER) values(?,?,?,?,?,?,?,?)";
		dao.insertDataRowBySql(sql, new Object[] {tableId, dataId, columnId, oldValue, newValue, batchNum, new Date(), userId});
	}


	public void addNumberLog(Long tableId, Long dataId, Long columnId, Object oldValue, Object newValue, Long batchNum, Long userId) throws Exception {
		String sql = "insert into tm_sys_data_log(TABLE_ID,DATA_ID,COLUMN_ID,OLD_VALUE_NUM,NEW_VALUE_NUM,BATCH_NUM,UPDATE_TIME,UPDATE_USER) values(?,?,?,?,?,?,?,?)";
		dao.insertDataRowBySql(sql, new Object[] {tableId, dataId, columnId, oldValue, newValue, batchNum, new Date(), userId});
	}
	
	public List<DataLog> getLogs(Long dataId, Long columnId) throws Exception {
		String sql = "select * from tm_sys_data_log where DATA_ID=? and COLUMN_ID=? order by UPDATE_TIME desc";
		List<Map<String, Object>> rows = dao.queryDataRowMapList(sql, new Object[] {dataId, columnId});
		List<DataLog> datalogs = new ArrayList<>();
		for(var row: rows) {
			RowMapper mapper = new RowMapper(row);
			DataLog log = new DataLog();
			log.setTableId(mapper.getLong("TABLE_ID"));
			log.setColumnId(columnId);
			log.setDataId(dataId);
			log.setNewValueDate(mapper.getLocalDate("NEW_VALUE_DATE"));
			log.setOldValueDate(mapper.getLocalDate("OLD_VALUE_DATE"));
			log.setNewValueTime(mapper.getLocalDateTime("NEW_VALUE_TIME"));
			log.setOldValueTime(mapper.getLocalDateTime("OLD_VALUE_TIME"));
			log.setNewValueNum(mapper.getBigDecimal("NEW_VALUE_NUM"));
			log.setOldValueNum(mapper.getBigDecimal("OLD_VALUE_NUM"));
			String newStr = mapper.getString("NEW_VALUE_STR");
			String oldStr = mapper.getString("OLD_VALUE_STR");
			if(newStr == null && oldStr == null) {
				newStr = mapper.getString("NEW_VALUE_TEXT");
				oldStr = mapper.getString("OLD_VALUE_STR");
			}
			log.setNewValueStr(newStr);
			log.setOldValueStr(oldStr);
			datalogs.add(log);
		}
		return datalogs;
	}
}
